<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <title>添加部门</title>
  <script type="text/javascript">
      Ext.onReady(function () {
          var parentId = Ext.create('Ext.form.field.Hidden', {
              name: 'parent_id',
              value: '<%=params[:parent_id]%>'
          });

          var companies = Ext.create('Ext.form.ComboBox', {
              fieldLabel: '所属公司',
              store: companyAllInfoStore,
              allowBlank: false,
              blankText: "公司必须选择",
              emptyText: '请选择...',
              valueField: 'id',
              displayField: 'name',
              name: 'company_id',
              loadingText: '搜索中...',
              minChars: 0,
              anchor: '100%',
              listConfig: {
                  loadingText: 'Searching...',
                  getInnerTpl: function () {
                      return '{name}';
                  }
              }
          });

          var name = Ext.create('Ext.form.field.Text', {
              fieldLabel: '名称',
              allowBlank: false,
              blankText: "部门名称不能为空",
              name: 'name',
              anchor: '100%'
          });

          var number = Ext.create('Ext.form.field.Text', {
              fieldLabel: '编号',
              allowBlank: false,
              blankText: "部门名称不能为空",
              name: 'number',
              anchor: '100%'
          });

          var departmentSubTypeStore = Ext.create('Ext.data.ArrayStore', {
              //fields: ['value', 'text'],
              fields: ['text'],
              data: departmentSubTypeArray
          });

          var departmentSubType = Ext.create('Ext.form.ComboBox', {
              fieldLabel: '部门类型',
              store: departmentSubTypeStore,
              valueField: 'text',
              displayField: 'text',
              typeAhead: true,
              mode: 'local',

              editable: false,

              name: 'sub_type',
              emptyText: '请选择部门类型...',
              allowBlank: false,
              anchor: '100%'
          });

          var desc = Ext.create('Ext.form.TextArea', {
              fieldLabel: '描述',
              name: 'description',
              height: 80,
              anchor: '100%'
          });

          var newDepartmentForm = Ext.create('Ext.form.Panel', {
              fieldDefaults: {
                  labelWidth: 55,
                  labelAlign: 'right'
              },
              url: '/department/create',
              frame: true,
              //title: '添加部门',
              bodyStyle: 'padding:5px 5px 0',
              width: 310,
              //height:300,
              items: [parentId, companies,name, number, departmentSubType, desc],
              buttons: [
                  {
                      text: '提交',
                      handler: function () {
                          // check form value
                          if (newDepartmentForm.form.isValid()) {
                              newDepartmentForm.form.submit({
                                  method: 'GET',
                                  success: function (result, response) {
                                      if (response.result.info == "成功") {
                                          //添加成功后，隐藏窗口，并刷新Grid
                                          newDepartmentWin.destroy();
                                          departmentTree.root.load();
                                      }
                                      else {
                                          Ext.MessageBox.alert('消息', response.result.info);
                                      }
                                  },
                                  failure: function (result, response) {
                                      Ext.MessageBox.alert('提示', result.responseText);
                                  }
                              });
                          }
                          else {
                              Ext.MessageBox.alert('错误提示', '请按要求填写必输选项.');
                          }
                      }
                  },
                  {
                      text: '取消',
                      handler: function () {
                          newDepartmentWin.destroy();
                      }
                  }
              ]
          });
          newDepartmentForm.render('new_department_form');
      })
  </script>
</head>
<body>
<div id="new_department_form"></div>
</body>
</html>
